package com.gesis.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.WebApps;
import org.zkoss.zk.ui.metainfo.ComponentInfo;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Chart;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.PieModel;
import org.zkoss.zul.SimplePieModel;

import com.gesis.delegate.DAParameters;
import com.gesis.delegate.DASuchen;
import com.gesis.entity.GesSubClassification;
import com.gesis.entity.GesTopic;
import com.gesis.utils.ExportXLS;
import com.gesis.utils.FacesUtils;
import com.sun.org.apache.bcel.internal.generic.GETSTATIC;

public class MBSuchen extends GenericForwardComposer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Listbox lstBeginYear;
	private Listbox lstEndYear;
	private Chart mychart;
	private Chart chartSoziologie;
	private Chart chartErziehungswissenschaft;
	private Chart chartPsychologie;
	private List<GesTopic> lstSuchen;
	private List<GesSubClassification> lstSoziologie;
	private List<GesSubClassification> lstErziehungswissenschaft;
	private List<GesSubClassification> lstPsychologie;
	private List<String[]> lstSoziologiePie;
	private List<String[]> lstErziePie;
	private List<String[]> lstPsychoPie;
	private int sozioTotal;
	private int erzieTotal;
	private int psychoTotal;
	private boolean hasPE;
	

	public ComponentInfo doBeforeCompose(Page page, Component parent,
			ComponentInfo compInfo) {
		this.initYearDropdown(page);
		hasPE = WebApps.getFeature("pe");
		page.setAttribute("hasPE", new Boolean(hasPE));
		return super.doBeforeCompose(page, parent, compInfo);
	}

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		syncModel();
		syncXLS();
	}

	private void initYearDropdown(Page page) {
		List<String> lstYear = new ArrayList<String>();
		try {
			lstYear = this.getAllYear();
		} catch (Exception e) {
			e.printStackTrace();
		}
		page.setAttribute("lstBYear", lstYear);
		page.setAttribute("lstEYear", lstYear);
	}

	public void onClick$search() {
		String bYear = lstBeginYear.getSelectedItem().getLabel();
		String eYear = lstEndYear.getSelectedItem().getLabel();
		try {
			lstSuchen = DASuchen.getInstance().searchForYear(bYear, eYear);
			lstSoziologie = DASuchen.getInstance().searchClassification("10200");
			lstPsychologie = DASuchen.getInstance().searchClassification("10700");
			lstErziehungswissenschaft = DASuchen.getInstance().searchClassification("10600");
			
			lstSoziologiePie = new ArrayList<String[]>();
			lstPsychoPie = new ArrayList<String[]>();
			lstErziePie = new ArrayList<String[]>();
			
			for (GesSubClassification subCla : lstSoziologie) {
				Long idCla = subCla.getGesClassification().getClaIdClassification();
				Long idTop = subCla.getSubIdTopic();
				int count = DASuchen.getInstance().countTopic(bYear, eYear, ""+idCla, ""+idTop);	
				if (count > 0) {
					lstSoziologiePie.add(new String[]{subCla.getSubDescClassification(),""+count});
					sozioTotal += count;
				}
			}
			
			for (GesSubClassification subCla : lstPsychologie) {
				Long idCla = subCla.getGesClassification().getClaIdClassification();
				Long idTop = subCla.getSubIdTopic();
				int count = DASuchen.getInstance().countTopic(bYear, eYear, ""+idCla, ""+idTop);	
				if (count > 0) {
					lstPsychoPie.add(new String[]{subCla.getSubDescClassification(),""+count});
					psychoTotal += count;
				}
			}
			
			for (GesSubClassification subCla : lstErziehungswissenschaft) {
				Long idCla = subCla.getGesClassification().getClaIdClassification();
				Long idTop = subCla.getSubIdTopic();
				int count = DASuchen.getInstance().countTopic(bYear, eYear, ""+idCla, ""+idTop);	
				if (count > 0) {
					lstErziePie.add(new String[]{subCla.getSubDescClassification(),""+count});
					erzieTotal += count;
				}
			}
			
			Session sess = Sessions.getCurrent(); 
			sess.setAttribute("pie", lstSuchen);
			sess.setAttribute("pieSozio", lstSoziologiePie);
			sess.setAttribute("totalPieSozio", sozioTotal);
			sess.setAttribute("pieErzie", lstErziePie);
			sess.setAttribute("totalPieErzie", erzieTotal);
			sess.setAttribute("piePsycho", lstPsychoPie);
			sess.setAttribute("totalPiePsycho", psychoTotal);
			syncModel();
			syncXLS();
			Executions.sendRedirect("result.zul");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void onClick$back(){
		Executions.sendRedirect("index.zul");
	}
	
	public void onClick$backR(){
		Executions.sendRedirect("result.zul");
	}
	
	public void onClick$graphics(){
		Executions.sendRedirect("graphics.zul");
	}

	private void syncModel() {
		if (!hasPE)
			return;
		
		Session sess = Sessions.getCurrent();
		lstSuchen = (ArrayList<GesTopic>) sess.getAttribute("pie");
		if (lstSuchen != null) {
			int size = lstSuchen.size();
			double countSoziologie = 0;
			double countErziehungswissenschaft = 0;
			double countPsychologie = 0;

			for (GesTopic gesTopic : lstSuchen) {
				Long id = gesTopic.getGesSubClassification()
						.getGesClassification().getClaIdClassification();
				if (id.equals(10200L)) {
					countSoziologie += 1;
				} else if (id.equals(10600L)) {
					countErziehungswissenschaft += 1;
				} else if (id.equals(10700L)) {
					countPsychologie += 1;
				}
			}
			
			
			
			PieModel model = new SimplePieModel();
			model.setValue("Soziologie", new Double(size > 0 ? (countSoziologie/size)*100 : 0));
			model.setValue("Erziehungswissenschaft", new Double(size > 0 ? (countErziehungswissenschaft/size)*100 : 0));
			model.setValue("Psychologie", new Double(size > 0 ? (countPsychologie/size)*100 : 0));
			mychart.setModel(model);
		}
		
		lstSoziologiePie = (ArrayList<String[]>) sess.getAttribute("pieSozio");
		if (lstSoziologiePie != null) {
			PieModel modelSozio = new SimplePieModel();
			sozioTotal = Integer.parseInt(sess.getAttribute("totalPieSozio").toString());			
			for (String[] subCount : lstSoziologiePie) {
				double countT = Double.parseDouble(subCount[1]);
				if (countT > 0) {
					modelSozio.setValue(subCount[0], new Double(sozioTotal > 0 ? (countT/sozioTotal)*100 : 0));
				}
			}
			chartSoziologie.setModel(modelSozio);
		}
		
		lstErziePie = (ArrayList<String[]>) sess.getAttribute("pieErzie");
		if (lstErziePie != null) {
			PieModel modelErzie = new SimplePieModel();
			erzieTotal = Integer.parseInt(sess.getAttribute("totalPieErzie").toString());			
			for (String[] subCount : lstErziePie) {
				double countT = Double.parseDouble(subCount[1]);
				if (countT > 0) {
					modelErzie.setValue(subCount[0], new Double(erzieTotal > 0 ? (countT/erzieTotal)*100 : 0));
				}
			}
			chartErziehungswissenschaft.setModel(modelErzie);
		}
		
		lstPsychoPie = (ArrayList<String[]>) sess.getAttribute("piePsycho");
		if (lstPsychoPie != null) {
			PieModel modelPsycho = new SimplePieModel();
			psychoTotal = Integer.parseInt(sess.getAttribute("totalPiePsycho").toString());			
			for (String[] subCount : lstPsychoPie) {
				double countT = Double.parseDouble(subCount[1]);
				if (countT > 0) {
					modelPsycho.setValue(subCount[0], new Double(psychoTotal > 0 ? (countT/psychoTotal)*100 : 0));
				}
			}
			chartPsychologie.setModel(modelPsycho);
		}
	}
	
	private void syncXLS(){
		ExportXLS xls = new ExportXLS();
		if (lstSuchen != null) {
			xls.generateXLSDate(lstSuchen);
		}
	}

	private List<String> getAllYear() throws Exception {
		return DAParameters.getInstance().loadYears();
	}
}
